unit clsCondFaturamento;

interface

uses Dm, dbtables, biblio, Sysutils;

type
  TCondFaturamento = class(TObject)
  private
    Conf_codigo: string;
  public
    Conf_descricao: string;
    Conf_perc1: double;
    conf_cod: string;
    function Gravar(p_Conf_codigo: string): Byte;
    function Ultimo: string;
    function Recuperar(p_Conf_codigo: string): Byte;
    function Recuperar_percentual(xpercentual: double): Byte;
    function Excluir(p_Conf_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
  end;

implementation

function TCondFaturamento.Gravar;

var
  tq: TQuery;

begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM CondFaturamento where Conf_codigo=:k_Conf_codigo');
  tq.parambyname('k_Conf_codigo').asstring := strzero(p_Conf_codigo, 2);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO CondFaturamento VALUES(:k_Conf_codigo,' +
      ':k_Conf_descricao,' +
      ':K_conf_perc1)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE CondFaturamento SET Conf_descricao=:k_Conf_descricao, ' +
      'Conf_perc1=:K_conf_perc1 ' +
      ' WHERE Conf_codigo=:k_Conf_codigo');
  end;
  tq.parambyname('k_Conf_codigo').asstring := strzero(p_Conf_codigo, 2);
  tq.parambyname('k_Conf_descricao').asstring := Conf_descricao;
  tq.parambyname('k_Conf_perc1').AsFloat := Conf_perc1;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TCondFaturamento.Ultimo;
var
  tq: TQuery;
  xmaior: integer;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT max(conf_codigo) as maior FROM CondFaturamento');
  tq.open;
  if trim(tq.fieldbyname('maior').asstring) = '' then begin
    result := '01';
  end else begin
    xmaior := tq.fieldbyname('maior').asinteger;
    if (xmaior < 99) then begin
      result := strzero(xmaior + 1, 2);
    end else begin
      Result := strzero(xmaior, 2);
    end;
  end;
  tq.Close;
  tq.Free;
end;

function TCondFaturamento.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM CondFaturamento where Conf_codigo=:k_Conf_codigo');
  tq.Params[0].asstring := strzero(p_Conf_codigo, 2);
  tq.open;
  Conf_codigo := p_Conf_codigo;
  if tq.eof then begin
    Conf_descricao := '';
    Conf_perc1 := 0;
    Result := 0;
  end else begin
    Conf_descricao := tq.fieldbyname('Conf_descricao').asstring;
    Conf_perc1 := tq.fieldbyname('Conf_perc1').AsFloat;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TCondFaturamento.Recuperar_Percentual;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM CondFaturamento where conf_perc1=:k_perc');
  tq.Params[0].asfloat := xpercentual;
  tq.open;
  if tq.eof then begin
    Conf_descricao := '';
    Conf_perc1 := 0;
    conf_cod := '';
    Result := 0;
  end else begin
    Conf_descricao := tq.fieldbyname('Conf_descricao').asstring;
    Conf_perc1 := tq.fieldbyname('Conf_perc1').AsFloat;
    conf_cod := tq.fieldbyname('conf_codigo').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TCondFaturamento.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM CondFaturamento WHERE Conf_codigo=:k_Conf_codigo');
  tq.parambyname('k_Conf_codigo').asstring := strzero(p_Conf_codigo, 2);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TCondFaturamento.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from CondFaturamento where Conf_descricao>=:k_Conf_descricao order by Conf_descricao');
  p_q.parambyname('k_Conf_descricao').asstring := p_nome;
end;

end.
